package com.epam.web_project.bank.command;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.epam.web_project.bank.entity.Customer;
import com.epam.web_project.bank.util.AppPropertiesLoader;

public class OnMainCommand implements Command {

	@Override
	public String execute(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		Customer customer = (Customer) session.getAttribute("customer");
		String resPage="";
		AppPropertiesLoader props = AppPropertiesLoader.getInstance();
		if (customer != null && !customer.isDeleted()) {
			if (!customer.isOnApproval()) {
				if ("customer".equals(customer.getRole())) { // customer
					resPage = props.getProperty("show_main_command");
				} else if ("admin".equals(customer.getRole())) { // admin
					resPage = props.getProperty("show_admin_main_command");
				}
			} else { // new unapproved customer
				resPage = props.getProperty("show_unnaproved_command");
			}
		} else { // deleted user or wrong login/password
			resPage = props.getProperty("login_page");
			session.invalidate();
		}
		return resPage;
	}

}
